﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Microsoft.Win32;
using System.ComponentModel;
using HWND = System.IntPtr;
using HANDLE = System.IntPtr;
using System.Windows.Forms;
using System.Drawing;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace UTIL
{
    public class CUtil
    {
        public static CUtil _util = null;
        public static CUtil Instance()
        {
            if (_util == null)
            {
                _util = new CUtil();
            }
            return _util;
        }
        public Object ReflectFunction(Object ct, String strFunctionName, Object[] args)
        {
            try
            {
                Type ty = ct.GetType();
                return ty.InvokeMember(strFunctionName, BindingFlags.InvokeMethod, Type.DefaultBinder, ct, args);

            }
            catch (Exception e)
            {

                CException ce = new CException();
                ce.SaveExcetpion(e);
                return null;
            }

        }

        /// <summary>
        /// 复制文件夹
        /// </summary>
        /// <param name="strSourceFolder"></param>
        /// <param name="strTargetFolder"></param>
        /// <returns></returns>
        public static int CopyFolder(IntPtr hwnd, String strSourceFolder, String strTargetFolder)
        {
            SHFILEOPSTRUCT pm = new SHFILEOPSTRUCT();
            pm.hwnd = hwnd;
            pm.wFunc = FO_COPY;
            pm.pFrom = strSourceFolder + "\0";
            pm.pTo = strTargetFolder + "\0";
            pm.lpszProgressTitle = "永胜数据库系统";
            pm.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR + FOF_NOCONFIRMATION;
            int ret = SHFileOperation(ref pm);
            if (ret == 0)
            {
                if (pm.fAnyOperationsAborted != 0)
                {
                    return 2;
                }
            }
            else
            {
                return ret;
            }
            return 0;
        }

        /// <summary>
        /// 复制文件夹
        /// </summary>
        /// <param name="strSourceFolder"></param>
        /// <param name="strTargetFolder"></param>
        /// <returns></returns>
        public static int CopyFolder(IntPtr hwnd, String strSourceFolder, String strTargetFolder,bool BExistOverride )
        {
            SHFILEOPSTRUCT pm = new SHFILEOPSTRUCT();
            pm.hwnd = hwnd;
            pm.wFunc = FO_COPY;
            pm.pFrom = strSourceFolder + "\0";
            pm.pTo = strTargetFolder + "\0";
            pm.lpszProgressTitle = "永胜数据库系统";
            if (BExistOverride)
            {
                pm.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR + FOF_NOCONFIRMATION;
            }
            else
            {
                pm.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR;
            }
            int ret = SHFileOperation(ref pm);
            if (ret == 0)
            {
                if (pm.fAnyOperationsAborted != 0)
                {
                    return 2;
                }
            }
            else
            {
                return ret;
            }
            return 0;
        }

        /// <summary>
        /// 复制文件夹
        /// </summary>
        /// <param name="strSourceFolder"></param>
        /// <param name="strTargetFolder"></param>
        /// <returns></returns>
        public static int DeleteFolder(IntPtr hwnd, String strSourceFolder)
        {
            SHFILEOPSTRUCT pm = new SHFILEOPSTRUCT();
            pm.hwnd = hwnd;
            pm.wFunc = FO_DELETE;
            pm.pFrom = strSourceFolder + "\0";
            pm.fFlags = FOF_NOCONFIRMATION;
            pm.lpszProgressTitle = "永胜数据库系统";
            int ret = SHFileOperation(ref pm);
            if (ret == 0)
            {
                if (pm.fAnyOperationsAborted != 0)
                {
                    return 2;
                }
            }
            else
            {
                return ret;
            }
            return 0;
        }

        public const int FOF_ALLOWUNDO = 0x40;
        public const int FOF_CONFIRMMOUSE = 0x2;
        public const int FOF_FILESONLY = 0x80;
        public const int FOF_MULTIDESTFILES = 0x1;
        public const int FOF_NOCONFIRMATION = 0x10;
        public const int FOF_NOCONFIRMMKDIR = 0x200;
        public const int FOF_RENAMEONCOLLISION = 0x8;
        public const int FOF_SILENT = 0x4;
        public const int FOF_SIMPLEPROGRESS = 0x100;
        public const int FOF_WANTMAPPINGHANDLE = 0x20;


        public const int FO_COPY = 0x2;
        public const int FO_DELETE = 0x3;
        public const int FO_MOVE = 0x1;
        public const int FO_RENAME = 0x4;

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 0)]
        public struct SHFILEOPSTRUCT
        {
            public HWND hwnd;
            public int wFunc;
            public string pFrom;
            public string pTo;
            public short fFlags;
            public int fAnyOperationsAborted;
            public HANDLE hNameMappings;
            public string lpszProgressTitle;
        }

        [DllImport("shell32", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern int SHFileOperation(ref SHFILEOPSTRUCT lpFileOp);
        [DllImport("shell32.dll")]
        public extern static IntPtr ShellExecute(IntPtr hwnd,
                                                 string lpOperation,
                                                 string lpFile,
                                                 string lpParameters,
                                                 string lpDirectory,
                                                 int nShowCmd
                                                );

        public static void  Delete(string path)
        {
            SHFILEOPSTRUCT lpFileOp = new SHFILEOPSTRUCT();
            lpFileOp.wFunc = FO_DELETE;
            lpFileOp.pFrom = path + "\0";
            lpFileOp.fFlags = FOF_NOCONFIRMATION  | FOF_SILENT;
            lpFileOp.fFlags &= ~FOF_ALLOWUNDO;
            lpFileOp.fAnyOperationsAborted = 0;

            int n = SHFileOperation(ref lpFileOp);
                     
        }

        public static void CopyFolder(string path,string strTo)
        {
            if (System.IO.Directory.Exists(path))
            {
                SHFILEOPSTRUCT lpFileOp = new SHFILEOPSTRUCT();
                lpFileOp.wFunc = FO_COPY;
                //lpFileOp.pFrom = path + "\0";
                //lpFileOp.pTo = strTo + "\0";
                lpFileOp.pFrom = path;
                lpFileOp.pTo = strTo;
                lpFileOp.fFlags = FOF_NOCONFIRMATION;
                //lpFileOp.fAnyOperationsAborted = 0;

                int n = SHFileOperation(ref lpFileOp);
            }

        }

        public enum ShowWindowCommands : int
        {

            SW_HIDE = 0,
            SW_SHOWNORMAL = 1,
            SW_NORMAL = 1,
            SW_SHOWMINIMIZED = 2,
            SW_SHOWMAXIMIZED = 3,
            SW_MAXIMIZE = 3,
            SW_SHOWNOACTIVATE = 4,
            SW_SHOW = 5,
            SW_MINIMIZE = 6,
            SW_SHOWMINNOACTIVE = 7,
            SW_SHOWNA = 8,
            SW_RESTORE = 9,
            SW_SHOWDEFAULT = 10,
            SW_MAX = 10
        }

        #region 判断是否有 XSC DSN 不存在创建 2009-09-19
        public static void CreateXscDsn()
        {
            Boolean bExist = false;
            try
            {
                RegistryKey regRootKey;
                RegistryKey regSubKey;
                regRootKey = Registry.LocalMachine;
                string strRegPath = @"SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources";
                regSubKey = regRootKey.OpenSubKey(strRegPath);
                string[] strDSNList = regSubKey.GetValueNames();
                regSubKey.Close();
                regRootKey.Close();
                if (strDSNList != null)
                {
                    foreach (String str in strDSNList)
                    {
                        if (str.ToUpper() == "XSC")
                        {
                            bExist = true;
                        }
                    }
                }

                if (!bExist)
                {

                    CreateODBC("XSC", "", "", "", "");
                }
            }
            catch (Exception)
            {

            }


        }

        /// <summary>
        /// ODBC
        /// </summary>
        /// <param name="dsn">dns</param>
        /// <param name="server">服务名</param>
        /// <param name="description">说明</param>
        /// <param name="uid">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        private static bool CreateODBC(string dsn, string server, string description, string uid, string pwd)
        {
            try
            {
                RegistryKey regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("ODBC").OpenSubKey("ODBC.INI", true).CreateSubKey(dsn);
                regKey.SetValue("BufferSize", "65535");  //默认
                regKey.SetValue("Description", description);
                regKey.SetValue("Driver", "%SystemRoot%\\system32\\sqliteodbc.dll"); //Oracle
                regKey.SetValue("DSN", dsn);
                regKey.SetValue("GuessTheColDef", "0");
                regKey.SetValue("PWD", pwd);
                regKey.SetValue("Remarks", "0");
                regKey.SetValue("SERVER", server);
                regKey.SetValue("StdDayOfWeek", "1");
                regKey.SetValue("StripTrailingZero", "0");
                regKey.SetValue("SynonymColumns", "1");
                regKey.SetValue("UID", uid);

                regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("ODBC").OpenSubKey("ODBC.INI", true).OpenSubKey("ODBC Data Sources", true);
                regKey.SetValue(dsn, "SQLite3 ODBC Driver"); //Oracle
                return true;
            }
            catch
            {
                return false;
            }
        }

        ///得到所有的ODBC驱动
        public static String[] GetDirver()
        {
            string[] strDSNList = null;
            try
            {
                RegistryKey regRootKey;
                RegistryKey regSubKey;
                regRootKey = Registry.LocalMachine;
                string strRegPath = @"SOFTWARE\ODBC\ODBCINST.INI\\ODBC Drivers";
                regSubKey = regRootKey.OpenSubKey(strRegPath);
                strDSNList = regSubKey.GetValueNames();
                regSubKey.Close();
                regRootKey.Close();
                return strDSNList;
            }
            catch (Exception)
            {
                return strDSNList;
            }

        }
        //SQLite3 ODBC Driver
        public static bool IsExistSqlLite3Driver()
        {
            String[] driver = GetDirver();
            bool bExist = false;
            foreach (String str in driver)
            {
                if (str == "SQLite3 ODBC Driver")
                {
                    bExist = true;
                }
            }
            return bExist;
        }
        #endregion


        //判断当前环境的版本
        public static bool FrameworkVersion()
        {
            bool bExist = false;
            try
            {
                RegistryKey regRootKey;
                RegistryKey regSubKey;
                regRootKey = Registry.LocalMachine;
                string strRegPath = @"SOFTWARE\Microsoft\NET Framework Setup\\NDP";
                regSubKey = regRootKey.OpenSubKey(strRegPath);
                if (regSubKey == null)
                    return bExist;
                string[] strDSNList = regSubKey.GetSubKeyNames();
                foreach (String str in strDSNList)
                {
                    if (str.Substring(0, 2).ToUpper().CompareTo("V2") >= 0)
                    {
                        bExist = true;
                        break;
                    }
                }
                regSubKey.Close();
                regRootKey.Close();
                regSubKey = null;
                regRootKey = null;
            }
            catch (Exception)
            {

            }
            return bExist;
        }
        public static void OpenFileSetFile(String strFilePath, String strFileName)
        {
            try
            {
                if (string.IsNullOrEmpty(strFilePath))
                {
                    return;
                }
                Process p = new Process();
                p.StartInfo.FileName = "EXPLORER.EXE";
                p.StartInfo.Arguments = " /select, " + strFilePath;
                p.StartInfo.UseShellExecute = true;
                p.Start();
            }
            catch (Exception me)
            {
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());
            }
        }

        //打开文件 
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="hwnd">句柄</param>
        /// <param name="strFilePath">文件的路径</param>
        public static void OpenFile(IntPtr hwnd, String strFilePath)
        {
            try
            {
                if (string.IsNullOrEmpty(strFilePath))
                {
                    return;
                }
                Process p = new Process();
                p.StartInfo.FileName = strFilePath;
                p.StartInfo.UseShellExecute = true;
                p.StartInfo.ErrorDialogParentHandle = hwnd;
                p.StartInfo.ErrorDialog = true;
                p.Start();
            }
            catch (Exception me)
            {
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 //D-2009-10-11
            }
        }

        //打开文件 
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="strFilePath">路径</param>
        public static void OpenFile(String strFilePath)
        {
            try
            {
                if (string.IsNullOrEmpty(strFilePath))
                {
                    return;
                }
                Process p = new Process();
                p.StartInfo.FileName = strFilePath;
                //特别修正 某些情况下报错 -设置当前的路径为程序运行的路径 A-2010-03-07
                String strExt = System.IO.Path.GetExtension(strFilePath).ToLower();
                String strCurrent = System.IO.Path.GetDirectoryName(strFilePath);
                String strCurrentOld = System.IO.Directory.GetCurrentDirectory();
                System.IO.Directory.SetCurrentDirectory(strCurrent);
                //A-2010-03-07

                p.StartInfo.UseShellExecute = true;
                p.StartInfo.ErrorDialog = true;
                p.Start();

                //A-2010-03-07 还原回去
                System.IO.Directory.SetCurrentDirectory(strCurrentOld);
                //A-2010-03-07
            }
            catch (Exception me)
            {
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
            }
        }

        //打开文件 
        /// <summary>
        /// 打开文件带参数
        /// </summary>
        /// <param name="strFilePath">文件的路径</param>
        /// <param name="strArgment">参数</param>
        public static void OpenFile(String strFilePath, String strArgment)
        {
            try
            {
                if (string.IsNullOrEmpty(strFilePath))
                {
                    return;
                }
                Process p = new Process();
                p.StartInfo.FileName = strFilePath;
                p.StartInfo.UseShellExecute = true;
                p.StartInfo.Arguments = strArgment;
                p.StartInfo.ErrorDialog = true;
                p.Start();
            }
            catch (Exception me)
            {
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
            }
        }

        //保存文件 打开保存文件对话框 保存文件
        /// <summary>
        /// 要保持的文件
        /// </summary>
        /// <param name="strFile"></param>
        public static String SaveFile(String strFileName, ADODB.Stream sr)
        {
            System.Windows.Forms.SaveFileDialog frmSaveDialog = new System.Windows.Forms.SaveFileDialog();
            frmSaveDialog.RestoreDirectory = true;
            frmSaveDialog.FileName = strFileName;
            frmSaveDialog.Filter = "(*" + System.IO.Path.GetExtension(strFileName) + ")|*" + System.IO.Path.GetExtension(strFileName);
            if (frmSaveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                sr.SaveToFile(frmSaveDialog.FileName, ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
                return frmSaveDialog.FileName;
            }
            else
            {
                return "";
            }
        }

        ///比较两个网页的BODY是否一样  A-2009-10-10
        static bool _bLoad = false;
        public static bool BTextSame(String strSource, String strTarget)
        {
            String SourceStr = "";
            String TargetStr = "";

            //两个相等
            if (String.Compare(strSource.TrimEnd(), strTarget.TrimEnd()) == 0)
            {
                return true;
            }

            using (System.Windows.Forms.WebBrowser wbSource = new System.Windows.Forms.WebBrowser())
            {
                wbSource.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(OnDocumentCompleted);

                _bLoad = true;
                wbSource.DocumentText = strSource;
                while (_bLoad)
                {
                    System.Windows.Forms.Application.DoEvents();
                }
                System.Threading.Thread.Sleep(20);
                System.Windows.Forms.HtmlElement hdSource = wbSource.Document.Body;
                if (hdSource != null)
                {
                    SourceStr = (hdSource.InnerHtml + "").TrimEnd();
                }


                _bLoad = true;
                wbSource.DocumentText = strTarget;
                while (_bLoad)
                {
                    System.Windows.Forms.Application.DoEvents();
                }
                System.Threading.Thread.Sleep(20);
                System.Windows.Forms.HtmlElement hdTarget = wbSource.Document.Body;

                if (hdTarget != null)
                {
                    TargetStr = (hdTarget.InnerHtml + "").TrimEnd();
                }

                if (String.Compare(SourceStr, TargetStr, true) == 0)
                {
                    return true;
                }
            }
            return false;
        }
        private static void OnDocumentCompleted(Object Sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser wb = Sender as WebBrowser;
            if (wb != null)
            {
                if (wb.ReadyState == WebBrowserReadyState.Complete)
                {
                    _bLoad = false;
                }
            }
            else
            {
                _bLoad = false;
            }
            System.Windows.Forms.Application.DoEvents();
        }
        /// <summary>
        /// 在一个SQL语句插入用户的SQL A-2009-10-11
        /// </summary>
        /// <param name="strSourceSql"></param>
        /// <param name="strUserName"></param>
        /// <returns></returns>
        public static String GetSqlWhere(String strSourceSql, String strUserName)
        {
            int iIndex = strSourceSql.ToLower().IndexOf(" where ");
            if (iIndex > 0)
            {
                String str1 = strSourceSql.Substring(0, iIndex);
                String str2 = strSourceSql.Substring(iIndex + 7);
                return str1 + " " + "  where  " + strUserName + " And " + str2;
            }
            else
            {
                return strSourceSql;
            }
        }
        /// <summary>
        /// 在一个SQL语句插入用户的SQL A-2009-10-11
        /// </summary>
        /// <param name="strSourceSql"></param>
        /// <param name="strUserName"></param>
        /// <returns></returns>
        public static String GetSqlLeftJoin(String strSourceSql, String strUserName)
        {

            int iIndex = strSourceSql.ToLower().IndexOf(" where ");
            if (iIndex > 0)
            {
                String str1 = strSourceSql.Substring(0, iIndex);
                String str2 = strSourceSql.Substring(iIndex + 7);
                return str1 + " " + "  where  " + strUserName + " And " + str2;
            }
            else
            {
                return strSourceSql;
            }
        }
        ///   <summary>   
        ///   返回星期中文名   
        ///   </summary>   
        ///   <param   name="WeekENG">星期英文名</param>   
        ///   <returns></returns>   
        public static string GetWeekCHA(string WeekENG)
        {
            string return_value = "";
            switch (WeekENG)
            {
                case "Monday":
                    return_value = "1";
                    return return_value;
                case "1":
                    return_value = "1";
                    return return_value;
                case "Tuesday":
                    return_value = "2";
                    return return_value;
                case "2":
                    return_value = "2";
                    return return_value;
                case "Wednesday":
                    return_value = "3";
                    return return_value;
                case "3":
                    return_value = "3";
                    return return_value;
                case "Thursday":
                    return_value = "4";
                    return return_value;
                case "4":
                    return_value = "4";
                    return return_value;
                case "Friday":
                    return_value = "5";
                    return return_value;
                case "5":
                    return_value = "5";
                    return return_value;
                case "Saturday":
                case "6":
                    return_value = "6";
                    return return_value;
                case "Sunday":
                    return_value = "7";
                    return return_value;
                case "0":
                    return_value = "7";
                    return return_value;
                default:
                    return_value = WeekENG;
                    return return_value;
            }
        }

        public static String ConvertText(object ob)
        {
            if (Convert.IsDBNull(ob))
            {
                return "";
            }
            if (null == ob)
            {
                return "";
            }
            return ob.ToString();
        }

        public static String GetReallyImage(String strPath, ref String strSourcePath)
        {
            try
            {
                if (!System.IO.File.Exists(strPath))
                {
                    return "";
                }

                string strImgFormat = "";
                using (System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(strPath))
                {
                    if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
                        strImgFormat = ".jpeg";
                    else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
                        strImgFormat = ".gif";
                    else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
                        strImgFormat = ".bmp";
                    else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
                        strImgFormat = ".png";
                    else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
                        strImgFormat = ".tiff";
                    else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon))
                        strImgFormat = ".icon";
                    else if (imgSrc.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Wmf))
                        strImgFormat = ".wmf";
                    else
                    {
                        strImgFormat = "";
                        return "";
                    }
                }

                String strImgFormat1 = System.IO.Path.GetExtension(strPath).ToLower();
                if (strImgFormat != strImgFormat1)
                {
                    try
                    {
                        String strDirName = System.IO.Path.GetDirectoryName(strPath);
                        String strName = System.IO.Path.GetFileNameWithoutExtension(strPath);
                        String strTarget = System.IO.Path.Combine(strDirName, strName);
                        strTarget = strTarget + strImgFormat;
                        if (System.IO.File.Exists(strTarget))
                        {
                            System.IO.File.Delete(strTarget);
                        }
                        System.IO.File.Copy(strPath, strTarget);
                        System.IO.File.Delete(strPath);
                        string strSourceDir9 = System.IO.Path.GetDirectoryName(strSourcePath);
                        string strSourceFile0 = System.IO.Path.GetFileNameWithoutExtension(strSourcePath);
                        strSourcePath = System.IO.Path.Combine(strSourceDir9, strSourceFile0);
                        strSourcePath = strSourcePath + strImgFormat;
                        return strTarget;
                    }
                    catch (Exception me)
                    {
                        UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
                        return "";
                    }
                }
                else
                {
                    return strPath;
                }
            }
            catch (Exception me)
            {
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
                return "";
            }
        }

        public void SetChildFormCenterParentForm(Form parentForm, Form childForm)
        {
            int left = parentForm.Left + (parentForm.Width) / 2 - childForm.Width / 2;
            int top = parentForm.Top + parentForm.Height / 2 - childForm.Height / 2;
            childForm.Left = left;
            childForm.Top = top;
        }

        public static bool IsImagePaath(String strPath)
        {
            try
            {
                strPath = strPath.ToLower();
                if (strPath.EndsWith(".jpeg") || strPath.EndsWith(".gif") || strPath.EndsWith(".bmp")
                    || strPath.EndsWith(".png") || strPath.EndsWith(".tiff") || strPath.EndsWith(".icon")
                    || strPath.EndsWith(".wmf") || strPath.EndsWith(".jpg"))
                {
                    return true;
                }
                return false;

            }
            catch (Exception me)
            {
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
                return false;
            }
        }

        public void CreateStausStrip(System.Windows.Forms.Form frmParent, ref System.Windows.Forms.StatusStrip su, ref System.Windows.Forms.ToolStripStatusLabel sup)
        {
            su = new System.Windows.Forms.StatusStrip();
            su.Dock = System.Windows.Forms.DockStyle.Bottom;
            frmParent.Height = frmParent.Height + su.Height;
            frmParent.Controls.Add(su);
            sup = new System.Windows.Forms.ToolStripStatusLabel();
            sup.Dock = System.Windows.Forms.DockStyle.Fill;
            sup.Text = "";
            su.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { sup });
            //设置字体
            CConfigXml ccx = new CConfigXml();
            ccx.LoadFile();
            String strFontName = ccx.GetAppSettings("FormFontName");
            System.Drawing.Font ft = new System.Drawing.Font(strFontName, su.Font.Size, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
            su.Font = ft;
            sup.Font = ft;
            //设置字体

        }
        public void RefreshTitle(System.Windows.Forms.StatusStrip su, string strAuthor, string strAddTime, string strTime, string strId)
        {
            if (su != null)
            {
                su.Items[0].Text = "当前记录编号为：" + strId.ToString() + " 添加时间为：" + strAddTime + " 最后一次修改时间为：" + strTime.ToString() + " 创建者为：" + strAuthor;
            }
        }

        public enum URL_PAR : uint
        {
            URL_PART_SCHEME = 1,
            URL_PART_HOSTNAME = 2,
            URL_PART_USERNAME = 3,
            URL_PART_PASSWORD = 4,
            URL_PART_PORT = 5,
            URL_PART_QUERY = 6
        }
        public static int URL_PARTFLAG_KEEPSCHEME = 0X1;
        [DllImport("shlwapi.dll", CharSet = CharSet.Auto)]
        static extern int UrlGetPart(
        [In]     string path,
        [Out]    StringBuilder url,
        [In, Out] ref int urlLength,
        [In]     int Part,
        [In]     int Flags
        );

        /// <summary>
        /// 得到网站的域名
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public static String GetDomain(String strUrl)
        {
            strUrl = strUrl.ToLower();
            if (!strUrl.StartsWith("http://"))
            {
                strUrl = "http://" + strUrl;
            }
            StringBuilder sb = new StringBuilder(1024);
            int urLenght = strUrl.Length;
            UrlGetPart(strUrl, sb, ref urLenght, (int)URL_PAR.URL_PART_HOSTNAME, URL_PARTFLAG_KEEPSCHEME);
            strUrl = sb.ToString();
            return sb.ToString();
        }

        ///得到网站收藏夹的图标
        public static byte[] GetFavicon(String strUrl)
        {
            String strBackUrl = strUrl;
            byte[] rs = null;
            try
            {

                strUrl = strUrl.ToLower();
                strUrl = strUrl.Replace("http:", "http://");
                strUrl = strUrl + "/favicon.ico";

                System.Net.HttpWebRequest hwr = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    System.Drawing.Image ig = System.Drawing.Image.FromStream(hwr.GetResponse().GetResponseStream());
                    ig.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    rs = ms.ToArray();
                }

                return rs;
            }
            catch (Exception)
            {
                try
                {
                    using (WebBrowser wb = new WebBrowser())
                    {
                        strBackUrl = strBackUrl.Replace("http:", "http://");
                        System.Net.HttpWebRequest hwr = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strBackUrl);
                        wb.DocumentStream = hwr.GetResponse().GetResponseStream();
                        while (wb.ReadyState != WebBrowserReadyState.Complete)
                        {
                            System.Threading.Thread.Sleep(1000);
                            System.Windows.Forms.Application.DoEvents();
                        }

                        HtmlDocument doc = wb.Document;
                        HtmlElementCollection collect = doc.GetElementsByTagName("link");

                        strUrl = "";
                        foreach (HtmlElement element in collect)
                        {
                            if (element.GetAttribute("rel") == "SHORTCUT ICON")
                                strUrl = element.GetAttribute("href");
                        }

                        if (collect.Count > 0 && strUrl != "")
                        {
                            hwr = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
                            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                            {
                                System.Drawing.Image ig = System.Drawing.Image.FromStream(hwr.GetResponse().GetResponseStream());
                                ig.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                                rs = ms.ToArray();
                            }
                        }
                        return rs;
                    }
                }
                catch (Exception me)
                {

                    UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
                    return null;
                }

            }
        }


        public void CenterForm(Form childForm)
        {
            Size size = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size;
            int left = (size.Width) / 2 - childForm.Width / 2;
            int top = +size.Height / 2 - childForm.Height / 2;
            childForm.Left = left;
            childForm.Top = top;
        }

        public enum 操作系统语言型 { 中文, 日文, 英文 }
        public static  操作系统语言型 Get操作系统语言()
        {
            String name = System.Threading.Thread.CurrentThread.CurrentCulture.Name.ToLower();
            if (name.StartsWith("zh-"))
            {
                return 操作系统语言型.中文;
            }
            else if (name.StartsWith("ja-"))
            {
                return 操作系统语言型.日文;
            }
            else
            {
                return 操作系统语言型.英文;
            }

        }

        #region 根据年月日计算星期几(Label2.Text=CaculateWeekDay(2004,12,9);)
        /**/
        /// <summary>
        /// 根据年月日计算星期几(Label2.Text=CaculateWeekDay(2004,12,9);)
        /// </summary>
        /// <param name="y">年</param>
        /// <param name="m">月</param>
        /// <param name="d">日</param>
        /// <returns></returns>
        public static string CaculateWeekDay(int y, int m, int d)
        {
            if (m == 1) m = 13;
            if (m == 2) m = 14;
            int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
            string weekstr = "";
            switch (week)
            {
                case 1: weekstr = "星期一"; break;
                case 2: weekstr = "星期二"; break;
                case 3: weekstr = "星期三"; break;
                case 4: weekstr = "星期四"; break;
                case 5: weekstr = "星期五"; break;
                case 6: weekstr = "星期六"; break;
                case 7: weekstr = "星期日"; break;
            }

            return weekstr;
        }
        #endregion

    }
  }
